package common

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func PrintListNode(head *ListNode) {
	var next *ListNode
	next = head
	for next != nil {
		fmt.Printf("%d ", next.Val)
		next = next.Next
	}
	fmt.Println("")
}

func ToSlice(head *ListNode) []int {
	var arr []int = make([]int, 0)
	next := head
	for next != nil {
		arr = append(arr, next.Val)
		next = next.Next
	}
	return arr
}

func Equals(head *ListNode, arr []int) bool {
	arr2 := ToSlice(head)
	if len(arr) != len(arr2) {
		return false
	}
	for i := 0; i < len(arr); i++ {
		if arr[i] != arr2[i] {
			return false
		}
	}
	return true
}

func BuildListNodeByCount(count int) *ListNode {
	var head *ListNode = &ListNode{}
	var next *ListNode = head
	for i := 0; i < count; i++ {
		p := ListNode{Val: i + 1}
		next.Next = &p
		next = &p
	}
	return head.Next
}

func BuildListNode(arr []int) *ListNode {
	var head *ListNode = &ListNode{}
	var next *ListNode = head
	for _, num := range arr {
		p := ListNode{Val: num}
		next.Next = &p
		next = &p
	}
	return head.Next
}
